unit help;
(*  cssMonkey
    Copyright (C) 2011 Matthew Hipkin <http://www.matthewhipkin.co.uk>

    This Source Code Form is subject to the terms of the Mozilla Public
    License, v. 2.0. If a copy of the MPL was not distributed with this
    file, You can obtain one at http://mozilla.org/MPL/2.0/. *)
{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, IpHtml, Forms, Controls, Graphics, Dialogs,
  ComCtrls, LCLIntF, miscfunc;

type

  { TfrmHelp }

  TfrmHelp = class(TForm)
    htmlView: TIpHtmlPanel;
    ToolBar1: TToolBar;
    procedure FormShow(Sender: TObject);
    procedure htmlViewHotClick(Sender: TObject);
  private
    { private declarations }
    procedure loadPage(url: String);
  public
    { public declarations }
    procedure HTMLGetImageX(Sender: TIpHtmlNode; const URL: string;
      var Picture: TPicture);
  end;

type
  TSimpleIpHtml = class(TIpHtml)
  public
    property OnGetImageX;
  end;

var
  frmHelp: TfrmHelp;

implementation

{$R *.lfm}

{ TfrmHelp }

procedure TfrmHelp.HTMLGetImageX(Sender: TIpHtmlNode; const URL: string;
  var Picture: TPicture);
var
  PicCreated: boolean;
  s: TMemoryStream;
begin
  s := TMemoryStream.Create;
  try
    PicCreated := false;
    httpGetStream(URL,s);
    if Picture = nil then
    begin
      Picture := TPicture.Create;
      Picture.LoadFromStream(s);
    end;
  except
    if PicCreated then Picture.Free;
    Picture := nil;
  end;
  s.Free;
end;

procedure TfrmHelp.loadPage(url: String);
var
  response: String;
  NewHTML: TSimpleIpHtml;
  html: TStringStream;
begin
  response := httpGet(url);
  html := TStringStream.Create(response);
  NewHTML := TSimpleIpHtml.Create;
  NewHTML.OnGetImageX := @HTMLGetImageX;
  NewHTML.LoadFromStream(html);
  htmlView.SetHtml(NewHTML);
  html.Free;
end;

procedure TfrmHelp.FormShow(Sender: TObject);
begin
  loadPage(HELPBASEURL + 'index.html');
end;

procedure TfrmHelp.htmlViewHotClick(Sender: TObject);
var
  NodeA: TIpHtmlNodeA;
begin
  if htmlView.HotNode is TIpHtmlNodeA then
  begin
    NodeA := TIpHtmlNodeA(htmlView.HotNode);
    if Copy(NodeA.HRef,1,Length(HELPBASEURL)) <> HELPBASEURL then OpenURL(NodeA.Href)
    else loadPage(NodeA.HRef);
  end;
end;

end.

